import { ref } from 'vue'
import { reqLogin } from '@/api/login'
import { userStore } from '@/stores/user'
import { useRouter } from 'vue-router'


export const Login = () => {
    const checked = ref()
    const router = useRouter()
    const countStore = userStore()
    const username = ref('')
    const password = ref('')
    const onSubmit = async () => {
        const data: any = await reqLogin(username.value, password.value)
        const { resultCode, resultInfo, token } = data
        
        
        countStore.saveCount(resultInfo)
        countStore.saveToken(token)
        if (resultCode === 1) {
            
            if (checked.value) {
                countStore.saveRcount(username.value,password.value)
            }else{
                countStore.delRcount()
            }
            router.push('/home')
        }
    }
    username.value = countStore.getRcount.username
    password.value = countStore.getRcount.password
    return { username, password, onSubmit, checked }
}

export const useRegister = () => {
    const router = useRouter()
    const goRegister = () => {
        router.push('/register')
    }
    return { goRegister }
}
